package com.ildepinc.persistence.concretes.daos;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import com.ildepinc.persistence.abstracts.DaoTableGeneric;
import com.ildepinc.persistence.abstracts.TableGeneric;
import com.ildepinc.persistence.concretes.models.Catalog;
import com.ildepinc.persistence.interfaces.JdbcConnectionBehaviour;

public class DaoCatalog extends DaoTableGeneric {

  public DaoCatalog() {
    this.clear();
  }

  public DaoCatalog(JdbcConnectionBehaviour connection, String schema) {
    this.clear();
    this.setJdbcConnection(connection);
    this.setTable(new Catalog(schema));
    this.setSqlCommand(this.getTable().getSqlCreateTable());
    this.setSqlParameters(new ArrayList<Object>());
  }

  @Override
  public TableGeneric copyRecordTo() {
    Catalog catalog = (Catalog) this.getTable();
    return catalog;
  }

  @Override
  public Integer doDelete(HashMap<String, Object> primaryKeys) throws Exception {
    Integer rowsAffected = null;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(primaryKeys.get("ID"));
    this.setSqlCommand(this.getTable().getSqlDelete());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(null);
    return rowsAffected;
  }

  @Override
  public Integer doInsert(TableGeneric record, HashMap<String, Object> primaryKeys) throws Exception {
    Integer rowsAffected = null;
    Catalog recordToInsert = (Catalog) record;
    this.getSqlParameters().clear();
    if( this.getTable().isPkSingleColumn() ) {
      Long newPk = this.getNewProgressive(
        this.getTable().getNewProgressiveMethod(),
        this.getTable().getSchema(),
        this.getTable().getTableName(),
        "ID"
      );
      recordToInsert.getPrimaryKeys().put("ID", newPk);
      this.getSqlParameters().add(newPk);
    }
    // if( (primaryKeys != null) && !primaryKeys.isEmpty() ) {
    //   this.getTable().setPrimaryKeys(primaryKeys);
    //   primaryKeys.forEach(
    //     (key, value)
    //       -> this.getSqlParameters().add(value)
    //   );
    // }
    this.getSqlParameters().add(recordToInsert.getName());
    this.getSqlParameters().add(recordToInsert.getDescription());
    this.setSqlCommand(this.getTable().getSqlInsert());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(recordToInsert);
    return rowsAffected;
  }

  @Override
  public TableGeneric doSelect(HashMap<String, Object> primaryKeys) throws Exception {
    Catalog readRecord = null;
    ResultSet resultSet = null;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(primaryKeys.get("ID"));
    this.setSqlCommand(this.getTable().getSqlSelect());
    resultSet = this.doManualSelect();
    resultSet.first();
    this.readRecordFrom(resultSet);
    readRecord = (Catalog) this.copyRecordTo();
    this.setTable(readRecord);
    return readRecord;
  }

  @Override
  public Integer doUpdate(TableGeneric record) throws Exception {
    Integer rowsAffected = null;
    Catalog recordToUpdate = (Catalog) record;
    this.getSqlParameters().clear();
    this.getSqlParameters().add(recordToUpdate.getName());
    this.getSqlParameters().add(recordToUpdate.getDescription());
    this.getSqlParameters().add(recordToUpdate.getId());
    this.setSqlCommand(this.getTable().getSqlUpdate());
    rowsAffected = this.doManualDmlOrDdl();
    this.setTable(recordToUpdate);
    return rowsAffected;
  }

  @Override
  public void readRecordFrom(ResultSet resultSet) throws Exception {
    Catalog catalog = (Catalog) this.getTable();
    catalog.setId(resultSet.getLong("ID"));
    catalog.getPrimaryKeys().put("ID", resultSet.getLong("ID"));
    catalog.setName(resultSet.getString("NAME"));
    catalog.setDescription(resultSet.getString("DESCRIPTION"));
    this.setTable(catalog);
  }

  @Override
  public void clear() {
    super.clear();
  }

}
